package com.sg.business.commons.editingsupport;

import com.sg.business.commons.ui.vault.DocumentUIUtils;
import com.sg.business.model.Deliverable;
import com.sg.business.model.Document;
import com.sg.business.model.IACLClient;
import com.sg.business.model.PermissionExtendsionTest;
import com.sg.business.model.Work;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.commons.editingsupport.CellClickListener;
import com.sg.widgets.part.CurrentAccountContext;

public class WorkViewDocEditingSupportor extends CellClickListener {

	@Override
	protected boolean canEdit(Object element) {
		if (element instanceof Deliverable) {
			try {
				Deliverable deliverable = (Deliverable) element;
				Document document = deliverable.getDocument();
				if (document == null) {
					return false;
				}
				Work work = deliverable.getWork();
				boolean skipPermission = work
						.canSkipDocumentPermission(new CurrentAccountContext());
				if (!skipPermission) {
					boolean b = PermissionExtendsionTest.test(document,
							IACLClient.CODE_READ);
					if (!b) {
						return false;
					}
				}
				return true;
			} catch (Exception e) {
			}
		}
		return false;
	}

	@Override
	protected void selected(Object element, String columnId) {
		Deliverable deliverable = (Deliverable) element;
		Document document = deliverable.getDocument();
		if (document == null) {
			return;
		}
		try {
			DocumentUIUtils.viewContent(document);
		} catch (Exception e) {
			MessageUtil.showToast(e);
		}
	}
}